// pages/member/qiandao/qiandao/index.js
import { PNT, setNavBarTitle, showToast, pushPagePath, scrollLoadList } from "../../../../utils/utils";
import { CheckDaka, DakaPage, DakaLog, DakaDoneBywx } from "../../../../utils/apis";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    moneyArr:[
      {},
      {money:100,num:100},
      {money:50000,num:365},
      {money:1800000,num:500},
      {money:4000000,num:800},
      {money:6000000,num:1000}
      ],
    title: [{}, { name: '赚钱通道一' }, { name: '赚钱通道二' }, { name: '赚钱通道三' }, { name: '赚钱通道四' }, { name: '赚钱通道五' }],
    msg2:['','满100元可提现','打卡满365次可提现','打卡满500次可提现','打卡满800次可提现','打卡满1000次可提现'],
    loading:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (opt) {
    this.loginModal = this.selectComponent("#login-modal");
    if (!this.loginModal.check()) {
      wx.showLoading({
        title: '请先登录',
      })
      return;
    }
    let msg = '';
    if(opt.type == 2){
      msg = '打卡报名费0.5元';
    }
    if(opt.type > 2){
      msg = '打卡报名费1元';
    }
    var info = wx.getStorageSync('userInfo')
    this.setData({
      type: opt.type || 1,
      info: info,
      msg:msg,
    });
    
    let title = this.data.title
    wx.setNavigationBarTitle({
      title: title[opt.type].name
    });
    this.dakaPage();
  },

  tixian(){
    var msg2 = this.data.msg2;
    
    showToast(msg2[this.data.type]);
  },
  gowu(){
    showToast('满100元可用于购物');
  },
//检测打卡
  checkData(e){
    // wx.vibrateLong({
    //   success: res => {
    //     console.log(res)
    //     console.log('颤抖')
    //   },
    //   fail: err => {
    //     console.log('我就问你为什么不抖动了')
    //   }
    // })
    if(this.data.pay){
      //支付
      wx.showModal({
        title: '温馨提示',
        content: this.data.msg,
        success(res) {
          if (res.confirm) {
            this.pay();
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return;
    }
    if(this.data.daka){
      showToast('您已打过卡,请明天再来','none');
      return;
    }
    var data = {};
    data.type = this.data.type;
    data.user_id = this.data.info.user_id;
    CheckDaka(data).then(res=>{
      
      if(res == 1){
        //支付
        wx.showModal({
          title: '温馨提示',
          content: this.data.msg,
          success(res) {
            if (res.confirm) {
              this.pay();
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        
      }else{
        this.dakaLog();
      }
    })
  },
  //获取打卡信息
  dakaPage(){
    var data = {};
    data.type = this.data.type;
    data.user_id = this.data.info.user_id;
    DakaPage(data).then(res=>{
      if(res.error_code == 10000){
        var moneyArr = this.data.moneyArr;
        var sign_money = ((moneyArr[this.data.type].money / moneyArr[this.data.type].num)*res.data.number).toFixed(0);
        console.log(res.data.number + "--" + moneyArr[this.data.type].num + "--" + moneyArr[this.data.type].money)
        this.setData({
          dakaInfo:res.data,
          // loading:res.data.loading,
          pay:res.data.pay,
          type:res.data.type,
          daka:res.data.daka,
          num:res.data.number,
          sign_money:sign_money
        })
        if(!res.data.loading){
          setTimeout(e=>{
            this.setData({
              loading:res.data.loading
            })
          },1500)
        }
        
      }
    })
  },
  //打卡逻辑
  dakaLog(e){
    var data = {};
    data.type = this.data.type;
    data.user_id = this.data.info.user_id;
    DakaLog(data).then(res => {
      console.log(res)
      if (res.error_code == 10000) {
        wx.vibrateLong({
          success: res => {
            console.log(res)
            console.log('颤抖')
          },
          fail: err => {
            console.log('我就问你为什么不抖动了')
          }
        })
        showToast('打卡成功', 'success')
        this.dakaPage();
      }else{
        showToast(res.msg,'none')
      }
    })
  },
rule(){
  wx.navigateTo({
    url: '/pages/member/qiandao/dakarule/dakarule',
  })
},
  //支付函数
  pay()
  {
    var data = {};
    data.user_id = this.data.info.user_id;
    console.log(this.data.info)
    data.openid = wx.getStorageSync('openid');
    data.type = this.data.type;
    DakaDoneBywx(data).then(res => {
      if (res.error_code == 10000) {
        wx.requestPayment({
          'timeStamp': res.info.timeStamp,
          'nonceStr': res.info.nonceStr,
          'package': res.info.package,
          'signType': 'MD5',
          'paySign': res.info.paySign,
          success: () => {
            showToast('支付成功', 'success');
            this.setData({
              pay:0
            })
            setTimeout(function () {
              this.checkData();
            }, 1500)
          },
          fail: () => {
            showToast('支付取消', 'warning');
          }
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})